package com.example.satoken.config;


import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.*;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {

    private List<Parameter> token = Collections.singletonList(new ParameterBuilder()
            .name("token").description("登录鉴权token").modelRef(new ModelRef("string"))
            .parameterType("header").required(true).build());
    private List<ResponseMessage> msg = Arrays.asList(
            new ResponseMessageBuilder().code(200).message("请求成功").build(),
            new ResponseMessageBuilder().code(500).message("服务异常").responseModel(new ModelRef("Result«boolean»")).build()
    );

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小蚁_接口文档")
                .description("小蚁接口文档")
                .contact(new Contact("swing", "192.168.0.65", "swing@yahoo.com"))
                .version("1.0.0")
                .build();
    }

    @Bean
    public Docket manageApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("后台系统文档")
                .enable(true)
                .apiInfo(apiInfo())
                .globalOperationParameters(token)
                .globalResponseMessage(RequestMethod.GET, msg)
                .globalResponseMessage(RequestMethod.POST, msg)
                .globalResponseMessage(RequestMethod.PUT, msg)
                .globalResponseMessage(RequestMethod.DELETE, msg)
                .consumes(new HashSet<String>(){{add("application/formData");}})
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swing"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.regex("/manage/.*"))
                .build();
    }
    @Bean
    public Docket appApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("移动APP文档")
                .enable(true)
                .apiInfo(apiInfo())
                .globalOperationParameters(token)
                .globalResponseMessage(RequestMethod.GET, msg)
                .globalResponseMessage(RequestMethod.POST, msg)
                .globalResponseMessage(RequestMethod.PUT, msg)
                .globalResponseMessage(RequestMethod.DELETE, msg)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swing"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.regex("/front/.*"))
                .build();
    }
    @Bean
    public Docket publicApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("公共接口")
                .enable(true)
                .apiInfo(apiInfo())
                .globalResponseMessage(RequestMethod.GET, msg)
                .globalResponseMessage(RequestMethod.POST, msg)
                .globalResponseMessage(RequestMethod.PUT, msg)
                .globalResponseMessage(RequestMethod.DELETE, msg)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swing"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.regex("/public/.*"))
                .build();
    }

}
